package com.hdax.dm.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hdax.dm.constant.ErrConstant;
import com.hdax.dm.dto.TicketbuyerDto;
import com.hdax.dm.entity.user.DmLinkUser;
import com.hdax.dm.exception.DmException;
import com.hdax.dm.mappers.LinkUserMapper;
import com.hdax.dm.mapstrucs.TicketbuyerDtoMapper;
import com.hdax.dm.service.LinkUserService;
import com.hdax.dm.utils.response.CommonResponse;
import com.hdax.dm.utils.response.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class  LinkUserSImpl extends ServiceImpl<LinkUserMapper,DmLinkUser> implements LinkUserService {

    @Autowired
    private TicketbuyerDtoMapper ticketbuyerDtoMapper;
    @Override
    public CommonResponse<List<TicketbuyerDto>> ticketbuyerlist(Integer uid) {
        QueryWrapper<DmLinkUser> wrapper=new QueryWrapper<>();
        wrapper.eq("userId",uid);
        return ResponseUtil.returnSuccess(ticketbuyerDtoMapper.toTargets(list(wrapper)));
    }

    @Override
    public CommonResponse<DmLinkUser> validatebuyerexist(String idCard) {
        QueryWrapper<DmLinkUser> wrapper=new QueryWrapper<>();
        wrapper.eq("idCard",idCard);
        DmLinkUser linkUser= getOne(wrapper);
        if(linkUser!=null){
            throw new DmException(ErrConstant.ErrCodeConstant.VALIDATE_BUYER_EXIST_CODE,ErrConstant.ErrMsgConstant.VALIDATE_BUYER_EXIST_MSG);
        }
        return ResponseUtil.returnSuccess(null,"购票人不存在");
    }

    @Override
    public CommonResponse<Integer> addticketbuyer(DmLinkUser linkUser,Integer uid) {
        linkUser.setUserId(Long.valueOf(uid));
        return ResponseUtil.returnSuccess(baseMapper.insert(linkUser),"添加成功");
    }

    @Override
    public CommonResponse<Integer> deleteticketbuyer(Integer id) {
        return ResponseUtil.returnSuccess(baseMapper.deleteById(id),"删除成功");
    }

    @Override
    public DmLinkUser linkUserOne(Long linkUserId,Long userId) {
        QueryWrapper<DmLinkUser> wrapper=new QueryWrapper<>();
        wrapper.eq("id",linkUserId);
        wrapper.eq("userId",userId);
        return getOne(wrapper);
    }
}
